package com.qingfeng.manager.controller;

import com.github.pagehelper.PageInfo;
import com.qingfeng.core.data.ResultData;
import com.qingfeng.core.entity.Userinfo;
import com.qingfeng.manager.service.UserinfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @description: 用户控制
 * @author:LWY
 * @date: 2017/10/27 17:49
 * @modified:
 */
@RestController
@RequestMapping("/userinfo")
public class UserinfoController {

    @Autowired
    UserinfoService userinfoService;

    /**
     * 获取所有的角色
     */
    @RequestMapping("/")
    public ResultData get(HttpServletRequest request) {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        if (StringUtils.isEmpty(pageNum) || StringUtils.isEmpty(pageSize)) {
            pageNum = "0";
            pageSize = "0";
        }
        String username = request.getParameter("username");
        String state = request.getParameter("state");
        PageInfo<Userinfo> all = userinfoService.get(Integer.valueOf(pageNum), Integer.valueOf(pageSize), username, state);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "成功", all);
        return resultData;
    }

    /**
     * 保存
     *
     * @return
     */
    @RequestMapping("/save")
    public ResultData save(HttpServletRequest request) {
        String id = request.getParameter("id");
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        Userinfo userinfo = userinfoService.save(id, username, pwd);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "保存成功", userinfo);
        return resultData;
    }


    /**
     * 删除
     *
     * @return
     */
    @RequestMapping("/del")
    public ResultData del(HttpServletRequest request) {
        String[] ids = request.getParameterValues("ids[]");
        userinfoService.del(ids);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "删除成功", null);
        return resultData;
    }

    /**
     * 保存授权
     *
     * @return
     */
    @RequestMapping("/saveRole")
    public ResultData saveRole(HttpServletRequest request) {
        String userinfo_id = request.getParameter("userinfo_id"); //用户id
        String[] role_ids = request.getParameterValues("role_ids[]");//权限id数组
        userinfoService.saveRole(userinfo_id, role_ids);
        ResultData resultData = new ResultData(ResultData.SUCCESS, "保存成功", "");
        return resultData;
    }
}
